/*
 * Piso.cpp
 *
 *  Created on: Sep 28, 2013
 *      Author: matias
 */

#include "Piso.h"
#include <GL/glew.h>
#include <GL/freeglut.h>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform2.hpp>
#include <glm/gtx/projection.hpp>

Piso::Piso(int size)
{
	this->grid_vertex_buffer_size = 12*(2*size +1);
	this->grid_vertex_buffer = new GLfloat[this->grid_vertex_buffer_size];

	this->grid_index_buffer_size = 4*(2*size +1);
	this->grid_index_buffer = new GLuint[this->grid_index_buffer_size];

	int offset;
	for (int i=0; i<size; i++)
	{
		offset = 24*i;
		this->grid_vertex_buffer[offset++] = -size;
		this->grid_vertex_buffer[offset++] = i+1;
		this->grid_vertex_buffer[offset++] = 0;

		this->grid_vertex_buffer[offset++] = size;
		this->grid_vertex_buffer[offset++] = i+1;
		this->grid_vertex_buffer[offset++] = 0;

		this->grid_vertex_buffer[offset++] = -size;
		this->grid_vertex_buffer[offset++] = -(i+1);
		this->grid_vertex_buffer[offset++] = 0;

		this->grid_vertex_buffer[offset++] = size;
		this->grid_vertex_buffer[offset++] = -(i+1);
		this->grid_vertex_buffer[offset++] = 0;


		this->grid_vertex_buffer[offset++] = i+1;
		this->grid_vertex_buffer[offset++] = -size;
		this->grid_vertex_buffer[offset++] = 0;

		this->grid_vertex_buffer[offset++] = i+1;
		this->grid_vertex_buffer[offset++] = size;
		this->grid_vertex_buffer[offset++] = 0;

		this->grid_vertex_buffer[offset++] = -(i+1);
		this->grid_vertex_buffer[offset++] = -size;
		this->grid_vertex_buffer[offset++] = 0;

		this->grid_vertex_buffer[offset++] = -(i+1);
		this->grid_vertex_buffer[offset++] = size;
		this->grid_vertex_buffer[offset++] = 0;
	}

	offset = 24 * size;
	this->grid_vertex_buffer[offset++]   = -size;
	this->grid_vertex_buffer[offset++] = 0;
	this->grid_vertex_buffer[offset++] = 0;

	this->grid_vertex_buffer[offset++] = size;
	this->grid_vertex_buffer[offset++] = 0;
	this->grid_vertex_buffer[offset++] = 0;

	this->grid_vertex_buffer[offset++] = 0;
	this->grid_vertex_buffer[offset++] = -size;
	this->grid_vertex_buffer[offset++] = 0;

	this->grid_vertex_buffer[offset++] = 0;
	this->grid_vertex_buffer[offset++] = size;
	this->grid_vertex_buffer[offset++] = 0;

	for (unsigned int i=0; i< this->grid_index_buffer_size; i++)
	{
		this->grid_index_buffer[i] = i;
	}
}

Piso::~Piso() {
	delete[] this->grid_index_buffer;
	delete[] this->grid_vertex_buffer;
}

/*Metodo alternativo por si la matriz del modelo es la llena de unos.
 *
 */
void Piso::render(GLuint handle){
	// Model Matrix
	glm::mat4* model_matrix = new glm::mat4(1.0f);
	this->render(handle, model_matrix);
	delete model_matrix; //libero la memoria
	return;
}

void Piso::render(GLuint handle, glm::mat4* matModelo){
	// Normal Matrix
	glm::mat3 normal_matrix = glm::mat3 ( 1.0f );

	// Bind Normal MAtrix
	GLuint location_normal_matrix = glGetUniformLocation( handle, "NormalMatrix");
	if( location_normal_matrix >= 0 )
	{
	    glUniformMatrix3fv( location_normal_matrix, 1, GL_FALSE, &normal_matrix[0][0]);
	}

	// Bind Model Matrix
	GLuint location_model_matrix = glGetUniformLocation( handle, "ModelMatrix");
	if( location_model_matrix >= 0 )
	{
		glUniformMatrix4fv( location_model_matrix, 1, GL_FALSE, &((*matModelo)[0][0]));
	}

	glEnableClientState(GL_VERTEX_ARRAY);

	glVertexPointer(3, GL_FLOAT, 0, this->grid_vertex_buffer);

	glDrawElements(GL_LINES, this->grid_index_buffer_size, GL_UNSIGNED_INT, this->grid_index_buffer);

	glDisableClientState(GL_VERTEX_ARRAY);
	return;
}
